/*
 * @(#)AbstractBaseObject.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.bean;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.sql.Timestamp;

/**
  AbstractBaseObject.java

  This class is the base bean class object.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   26/06/2003
    @version         $Revision: 1.6.32.1 $
*/

public abstract class AbstractBaseObject implements java.io.Serializable {
  protected Integer id = null;
  protected Integer updateCount = null;
  protected String recordStatus = null;
  protected Integer updaterID = null;
  protected Timestamp updateDate = null;
  protected Integer creatorID = null;
  protected Timestamp createDate = null;

  protected String updaterName = null;
  protected String creatorName = null;

  protected int recordCount = 0;
  protected int rowNum = 0;

  //Used for pager Netivgator, when applied permission
  protected int prvPageRowNum = 0;
  protected int nxtPageRowNum = 0;
  protected boolean nxtPageAvailable = false;

  public Integer getID() {
    return(this.id);
  }

  public void setID(Integer id) {
    this.id = id;
  }

  public Integer getUpdateCount() {
    return(this.updateCount);
  }

  public void setUpdateCount(Integer updateCount) {
    this.updateCount = updateCount;
  }

  public String getRecordStatus() {
    return(this.recordStatus);
  }

  public void setRecordStatus(String recordStatus) {
    this.recordStatus = recordStatus;
  }

  public Integer getUpdaterID() {
    return(this.updaterID);
  }

  public void setUpdaterID(Integer updaterID) {
    this.updaterID = updaterID;
  }

  public Timestamp getUpdateDate() {
    return(this.updateDate);
  }

  public void setUpdateDate(Timestamp updateDate) {
    this.updateDate = updateDate;
  }

  public Integer getCreatorID() {
    return(this.creatorID);
  }

  public void setCreatorID(Integer creatorID) {
    this.creatorID = creatorID;
  }

  public Timestamp getCreateDate() {
    return(this.createDate);
  }

  public void setCreateDate(Timestamp createDate) {
    this.createDate = createDate;
  }

  public String getUpdaterName() {
    return(this.updaterName);
  }

  public void setUpdaterName(String updaterName) {
    this.updaterName = updaterName;
  }

  public String getCreatorName() {
    return(this.creatorName);
  }

  public void setCreatorName(String creatorName) {
    this.creatorName = creatorName;
  }

  public int getRecordCount() {
    return(this.recordCount);
  }

  public void setRecordCount(int recCnt) {
    this.recordCount = recCnt;
  }

  public int getRowNum() {
    return(this.rowNum);
  }

  public void setRowNum(int rowNum) {
    this.rowNum = rowNum;
  }

  public void setPrvPageRowNum(int prvPageRowNum){
    this.prvPageRowNum = prvPageRowNum;
  }

  public int getPrvPageRowNum(){
    return this.prvPageRowNum;
  }

  public void setNxtPageRowNum(int nxtPageRowNum){
    this.nxtPageRowNum = nxtPageRowNum;
  }

  public int getNxtPageRowNum(){
    return this.nxtPageRowNum;
  }


  public void setNxtPageAvailable(boolean nxtPageAvailable) {
    this.nxtPageAvailable = nxtPageAvailable;
  }

  public boolean getNxtPageAvailable() {
    return this.nxtPageAvailable;
  }

  protected String toString(Object val) {
    return (val == null ? "<null>" : val.toString());
  }

  public Object clone(){
    Object deepCopy = null;
    try {
      // serialize me into byte array
      java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(100);
      java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
      oos.writeObject(this);
      byte buf[] = baos.toByteArray();
      oos.close();

      // deserialize byte array into a new object
      java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(buf);
      java.io.ObjectInputStream ois = new java.io.ObjectInputStream(bais);
      deepCopy = ois.readObject();
      ois.close();
    } catch (Exception e) {
      //ATTENTION: This is special case. DO NOT covert it to logger since it would cause InvalidClassException.
      e.printStackTrace();
    }
    return deepCopy;
  }

  public String toString() {
    Class cl = this.getClass();
    String s = toString(this, cl).toString();
    return cl.getName() + "[" + s.substring(0, s.length() - 1) + "]";
  }

  protected StringBuffer toString(Object obj, Class cls){
    StringBuffer buff = new StringBuffer(" ");
    Field[] fields = cls.getDeclaredFields();
    try {
      AccessibleObject.setAccessible(fields,true);
      for (int i = 0; i<fields.length; i++) {
        buff.append(fields[i].getName()).append(":").append((fields[i].get(obj) == null ? "<null>" : fields[i].get(obj).toString())).append("|");
      }
    } catch (Exception e) {}
    Class superClass = cls.getSuperclass();
    if (superClass!=null && !"java.lang.Object".equals(superClass.getName())){
      return toString(obj,superClass).append(buff);
    } else {
      return buff;
    }
  }

  public boolean equals(Object obj) {
    return(obj != null && obj.getClass().getName().equals(this.getClass().getName()) && ((AbstractBaseObject)obj).getID() != null && ((AbstractBaseObject)obj).getID().equals(this.getID()));
  }
}
